import http from '../../api/http.js'
import { requestData } from '../../common/js/commont.js'
// 登录
const login = (data) => http.post('/api/users/register', requestData(data))
// 获取点位信息
const getMapinfo = (data) => http.post('/api/map/getMapInfo', requestData(data))
// 获取历史数据曲线用
const getHistorydeviceLog = (data) => http.post('/api/Devicelog/getHistoryDeviceLog', requestData(data))

const modules = {
  state: {
    User: null,
    MapInfo: null,
    HistorydeviceLog: null
  },
  mutations: {
    setUser (state, data) {
      state.User = data
      window.localStorage.setItem('token', data.token)
      window.localStorage.setItem('account', data.name)
    },
    setMapInfo (state, data) {
      state.MapInfo = data
    },
    setHistorydeviceLog (state, data) {
      state.HistorydeviceLog = data
    }
  },
  actions: {
    async Login ({ commit }, data) {
      try {
        var res = await login(data)
        commit('setUser', res.data.data)
        return res
      } catch (e) {
        throw new Error(e)
      }
    },
    async getMapInfo ({ commit }, data) {
      try {
        var res = await getMapinfo(data)
        commit('setMapInfo', res.data.data)
        return res
      } catch (e) {
        throw new Error(e)
      }
    },
    async getHistoryDeviceLog ({ commit }, data) {
      try {
        var res = await getHistorydeviceLog(data)
        commit('setHistorydeviceLog', res.data.data)
        return res
      } catch (e) {
        throw new Error(e)
      }
    }
  }
}
export default modules
